﻿/**
 * The AudioTrack Library by Chris J. Rock. Aug 10, 2011
 * Check out chrisjrock.net and be a king among men.
 *
 * Copyright (c) 2011 Chris J. Rock
 * 
 * Permission is hereby granted, free of charge, to any person
 * obtaining a copy of this software and associated documentation
 * files (the "Software"), to deal in the Software without
 * restriction, including without limitation the rights to use,
 * copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following
 * conditions:
 * 
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 * OTHER DEALINGS IN THE SOFTWARE.
 **/


package net.chrisjrock.sound.audioTrack.track 
{
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.media.SoundTransform;
	
	import net.chrisjrock.sound.audioTrack.data.AudioTrackData;
	import net.chrisjrock.sound.audioTrack.events.AudioMixerEvent;
	import net.chrisjrock.sound.audioTrack.events.AudioTrackEvent;
	
	/**
	 * A track used to manage Movieclip sound.
	 * @author Christopher J. Rock
	 */
	//TODO: Test this class and improve functionality!
	public class MovieClipTrack extends AbstractTrack implements ISequencedTrack 
	{
		private var _input:MovieClip;
		
		private var _playing:Boolean;
		
		private var _autoPlay:Boolean;
		private var _startPos:uint;
		private var _loop:Boolean = true;
		
		private var _prevFrame:int;
		
		public function MovieClipTrack(trackId:String , input:MovieClip = null , _autoPlay:Boolean = false) 
		{
			super(trackId);
			
			_autoPlay = _autoPlay;
			
			if (input)
			{
				setInput(input);
			}
		}
		
		/**
		 * @inheritDoc
		 */
		public function stop():void 
		{
			if (_playing) 
			{
				if (_input)
				{
					_input.gotoAndStop(_startPos);
				}
				
				_playing = false;
				dispatchEvent(new AudioTrackEvent(AudioTrackEvent.AT_START));
				dispatchEvent(new AudioTrackEvent(AudioTrackEvent.STOP));
			}
		}
		
		/**
		 * @inheritDoc
		 */
		public function play (restart:Boolean = false):void 
		{
			if (restart || !_playing) 
			{
				_playing = true;
				if (restart) 
				{
					_input.gotoAndPlay(_startPos);
					dispatchEvent(new AudioTrackEvent(AudioTrackEvent.PLAY));
					if (_input.currentFrame == _startPos)
					{
						dispatchEvent(new AudioTrackEvent(AudioTrackEvent.LEAVE_START));
					}
				}
				else 
				{
					_input.play();
					dispatchEvent(new AudioTrackEvent(AudioTrackEvent.PLAY));
				}
			}
		}
		
		/**
		 * @inheritDoc
		 */
		public function pause():void 
		{
			if (_playing) 
			{
				if (_input)
				{
					_input.stop();
				}
				
				_playing = false;
				dispatchEvent(new AudioTrackEvent(AudioTrackEvent.PAUSE));
			}
		}
		
		/**
		 * @inheritDoc
		 */
		public function scrubTo (position:Number):void 
		{
			if (_input == null)
			{
				return;
			}
			
			if (_playing)
			{
				_input.gotoAndPlay(position);
			}
			else
			{
				_input.gotoAndStop(position);
			}
		}
		
		/**
		 * Sets the source MovieClip object. 
		 * @param input
		 * 
		 */		
		public function setInput (input:MovieClip = null):void 
		{
			stop();
			if (_input)
			{
				_input.removeEventListener(Event.ENTER_FRAME , enterFrameHandler);
			}
			
			_input = input;
			
			if (_input) 
			{
				_input.addEventListener(Event.ENTER_FRAME , enterFrameHandler);
				updateTransform();
				_input.stop();
				_prevFrame = _input.currentFrame;
				
				if (_autoPlay)
				{
					play();
				}
			}
		}
		
		private function enterFrameHandler (event:Event):void 
		{
			if (!_playing) 
			{
				return;
			}
			
			if (_prevFrame == _startPos && _input.currentFrame != _startPos)
			{
				dispatchEvent(new AudioTrackEvent(AudioTrackEvent.LEAVE_START));
			}
			else if (_prevFrame == _input.totalFrames && _input.currentFrame != _input.totalFrames)
			{
				dispatchEvent(new AudioTrackEvent(AudioTrackEvent.LEAVE_END));
			}
			
			if (_prevFrame != _startPos && _input.currentFrame == _startPos)
			{
				dispatchEvent(new AudioTrackEvent(AudioTrackEvent.AT_START));
			}
			
			if (_prevFrame != _input.totalFrames && _input.currentFrame == _input.totalFrames) 
			{
				dispatchEvent(new AudioTrackEvent(AudioTrackEvent.AT_END));
				if (_playing && !_loop)
				{
					pause();
				}
			}
			
			_prevFrame = _input.currentFrame;
		}
		
		/**
		 * @inheritDoc
		 */
		override public function dispose():void 
		{
			if (_input) 
			{
				_input.removeEventListener(Event.ENTER_FRAME , enterFrameHandler);
				_input = null;
			}
			
			super.dispose();
		}
		
		/**
		 * @inheritDoc
		 */
		override public function updateTransform (transform:SoundTransform = null):void 
		{
			super.updateTransform(transform);
			
			if (_input)
			{
				_input.soundTransform = getTransform();
			}
		}
		
		/**
		 * Returns the source MovieClip object.
		 */
		public function get input ():MovieClip 
		{
			 return _input; 
		}
		
		/**
		 * @inheritDoc
		 */
		public function get playing ():Boolean 
		{
			 return _playing; 
		}
		
		/**
		 * @inheritDoc
		 */
		public function get paused ():Boolean 
		{
			 return !_playing && _input && _input.currentFrame == _startPos; 
		}
		
		/**
		 * @inheritDoc
		 */
		public function get position ():Number 
		{
			if (_input == null)
			{
				return NaN;
			}
			
			 return _input.currentFrame; 
		}
		
		/**
		 * @inheritDoc
		 */
		public function get duration ():Number 
		{
			if (_input == null)
			{
				return NaN;
			}
			
			 return _input.totalFrames; 
		}

		public function get autoPlay():Boolean
		{
			return _autoPlay;
		}

		public function set autoPlay(value:Boolean):void
		{
			_autoPlay = value;
		}

		public function get startPos():uint
		{
			return _startPos;
		}

		public function set startPos(value:uint):void
		{
			_startPos = value;
		}

		public function get loop():Boolean
		{
			return _loop;
		}

		public function set loop(value:Boolean):void
		{
			_loop = value;
		}

		
	}
	
}